package me.android.library.utils.android;

import android.annotation.SuppressLint;
import android.os.Environment;

import com.google.common.base.Strings;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;
import mylab.utils.common.FileUtil;


@Slf4j
@UtilityClass
public class LogUtil {

    public final String DEFAULT_PATH = "log";
    public final String DEFAULT_File = "log";
    public final String DEFAULT_SUFFIX = ".txt";
    @SuppressLint("SimpleDateFormat")
    final SimpleDateFormat SDF_DATE = new SimpleDateFormat("yyyy-MM-dd");
    @SuppressLint("SimpleDateFormat")
    final SimpleDateFormat SDF_TIME = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

    public void logFileWithTime(String content) {
        logFileWithTime(getPath(), content);
    }

    public void logFileWithTime(String path, String content) {
        logFileWithTime(path, content, true);
    }

    public void logFileWithTime(String path, String content, boolean isAppend) {

        String time = SDF_TIME.format(Calendar.getInstance().getTime());
        content = String.format("%s:\t %s\n", time, content);
        logFile(path, content, isAppend);
    }

    public void logFile(String path, String content, boolean isAppend) {
        if (Strings.isNullOrEmpty(content)) return;
        FileUtil.writeFile(content, path, isAppend);
    }


    String getPath() {

        return Environment.getExternalStorageDirectory()
                .getPath()
                .concat(File.separator)
                .concat(DEFAULT_PATH)
                .concat(File.separator)
                .concat(DEFAULT_File)
                .concat(String.format("_%s", SDF_DATE.format(Calendar.getInstance().getTime())))
                .concat(DEFAULT_SUFFIX);
    }
}
